#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#--------------------
set -e

#--------------------
# common defines
ARCH=$1
UNAME_SM=$(uname -sm)
echo "build on $UNAME_SM"
echo "ARCH=$ARCH"
if [ -z "$ARCH" ]; then
    echo "You must specific an architecture 'arm, armv7a, x86, ...'."
    echo ""
    exit 1
fi

echo "ANDROID_NDK=$ANDROID_NDK"

if [ -z "$ANDROID_NDK" ]; then
    echo "You must define ANDROID_NDK before starting."
    echo "They must point to your NDK directories."
    echo ""
    exit 1
fi

# Only choose one of these, depending on your build machine...
export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
export SYSROOT=$TOOLCHAIN/sysroot
# Set this to your minSdkVersion.
export API=21

#----- arch begin -----
if [ "$ARCH" = "armv7a" ]; then
    # depending on your device...
    export TARGET="armv7a-linux-androideabi"
    # Configure and build.
    # export CFLAGS="-fPIE -fPIC"
    # export LDFLAGS="-pie"
    export CFLAGS="-mthumb -O3 -fomit-frame-pointer"
    export CXXFLAGS="-mthumb -O3 -fomit-frame-pointer"
    export ABI=armeabi-v7a

elif [ "$ARCH" = "arm64" ]; then
    # Set this to your minSdkVersion.
    export API=21
    # depending on your device...
    export TARGET="aarch64-linux-android"
    # Configure and build.
    # export CFLAGS="-fPIE -fPIC"
    # export LDFLAGS="-pie"
    # export LDFLAGS=-L$SYSROOT/usr/lib/$TARGET/$API
    export CFLAGS="-O3 -fomit-frame-pointer"
    export CXXFLAGS="-O3 -fomit-frame-pointer"
    export ABI=arm64-v8a

elif [ "$ARCH" = "x86" ]; then
    # depending on your device...
    export TARGET="i686-linux-android"
    # Configure and build.
    # export CFLAGS="-fPIE -fPIC"
    # export LDFLAGS="-pie"
    export CFLAGS="-O3 -fomit-frame-pointer"
    export CXXFLAGS="-O3 -fomit-frame-pointer"
    export ABI=x86

elif [ "$ARCH" = "x86_64" ]; then
    # Set this to your minSdkVersion.
    export API=21
    # depending on your device...
    export TARGET="x86_64-linux-android"
    # Configure and build.
    # export CFLAGS="-fPIE -fPIC"
    # export LDFLAGS="-pie"
    export CFLAGS="-O3 -fomit-frame-pointer"
    export CXXFLAGS="-O3 -fomit-frame-pointer"
    export ABI=x86_64

else
    echo "unknown architecture $ARCH";
    exit 1
fi

# output build
export PREFIX=$BUILD_ROOT/build/$ABI

# Prefix of build tools
if [[ $TARGET == "armv7a-linux-androideabi" ]]; then
    export TOOL_PREFIX="arm-linux-androideabi"
else
    export TOOL_PREFIX=$TARGET
fi

#--------------------
echo "TOOL_PREFIX=$TOOL_PREFIX"
echo "ABI=$ABI"
echo "API=$API"

# try to detect NDK version
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export AR=$TOOLCHAIN/bin/$TOOL_PREFIX-ar
export AS=$TOOLCHAIN/bin/$TOOL_PREFIX-as
export LD=$TOOLCHAIN/bin/$TOOL_PREFIX-ld
export NM=$TOOLCHAIN/bin/$TOOL_PREFIX-nm
export STRIP=$TOOLCHAIN/bin/$TOOL_PREFIX-strip
export RANLIB=$TOOLCHAIN/bin/$TOOL_PREFIX-ranlib

#export CPPFLAGS="-DNDEBUG"
#export LIBS="-lstdc++"

if [ -e "$ANDROID_NDK/RELEASE.TXT" ]; then
  ANDROID_NDK_REL=$(grep -o '^r[0-9]*.*' $ANDROID_NDK/RELEASE.TXT 2>/dev/null | sed 's/[[:space:]]*//g' | cut -b2-)
elif [ -e "$ANDROID_NDK/source.properties" ]; then
  ANDROID_NDK_REL=$(grep -o '^Pkg\.Revision.*=.*[0-9]*.*' $ANDROID_NDK/source.properties 2>/dev/null | sed 's/[[:space:]]*//g' | cut -d "=" -f 2)
else
  exit 1
fi

case "$ANDROID_NDK_REL" in
    23*|24*|25*)
        echo "ANDROID_NDK_REL=$ANDROID_NDK_REL (LLD)"
        echo "Build will successed, but run with error below:"
        echo "dlopen failed: cannot locate symbol \"__aarch64_ldadd8_acq_rel\""
        echo "dlopen failed: cannot locate symbol \"__floatditf\""
        echo "https://zhuanlan.zhihu.com/p/577615755"
        exit 1
        #export CFLAGS="-mno-outline-atomics $CFLAGS"
        #export CXXFLAGS="-mno-outline-atomics $CXXFLAGS"
        export LD=$TOOLCHAIN/bin/ld
        export AR=$TOOLCHAIN/bin/llvm-ar
        export AS=$CC
        export NM=$TOOLCHAIN/bin/llvm-nm
        export STRIP=$TOOLCHAIN/bin/llvm-strip
        export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
        ;;
    22*)
        echo "ANDROID_NDK_REL=$ANDROID_NDK_REL (LLD)"
        export LD=$TOOLCHAIN/bin/ld
        export AR=$TOOLCHAIN/bin/llvm-ar
        export AS=$TOOLCHAIN/bin/llvm-as
        export NM=$TOOLCHAIN/bin/llvm-nm
        export STRIP=$TOOLCHAIN/bin/llvm-strip
        export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
        ;;
    *)
        echo "ANDROID_NDK_REL=$ANDROID_NDK_REL"
        ;;
esac

#--------------------
echo "TOOLCHAIN=$TOOLCHAIN"
echo "CC=$(basename $CC)"
echo "CXX=$(basename $CXX)"
echo "AR=$(basename $AR)"
echo "AS=$(basename $AS)"
echo "LD=$(basename $LD)"
echo "NM=$(basename $NM)"
echo "STRIP=$(basename $STRIP)"
echo "RANLIB=$(basename $RANLIB)"
echo "CFLAGS=$CFLAGS"
echo "CXXFLAGS=$CXXFLAGS"
echo "CPPFLAGS=$CPPFLAGS"
echo "LIBS=$LIBS"
echo "PREFIX=$PREFIX"
echo "========================================"
